//
//  useView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/5.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import TYAttributedLabel
class UseView: UIView, SBPlayerDelegate, AddCornerUIRectCorner, BottomShadow {

    @IBOutlet weak var userInfoBttomView: UIView!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var avatarImageView: BaseCircleImageView!
    @IBOutlet weak var userInfoBgImageView: UIImageView!
    let sideMargin: CGFloat = 27
    @IBOutlet weak var userInfoHeight: NSLayoutConstraint!
    
    @IBOutlet weak var userInfoView: UIView!
    
    @IBOutlet weak var followButton: UIButton!
    @IBOutlet weak var levelLabel: UILabel!
    @IBOutlet weak var genderImageView: UIImageView!
    @IBOutlet weak var bPlayer: SBPlayer!
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var introductionLabel: TYAttributedLabel!
    @IBOutlet weak var titleLabelHeight: NSLayoutConstraint!
    @IBOutlet weak var introductionHeight: NSLayoutConstraint!
    @IBOutlet weak var titleLabel: TYAttributedLabel!
    @IBOutlet weak var tagScrollView: TagScrollView!
    @IBOutlet weak var itemsCollectionView: ConsultationCollectionView!
    @IBOutlet weak var commentCountLabel: UILabel!
    @IBOutlet weak var commentCountView: UIView!
    var videoADView: VideoADView?
    /// 通过xib获取当前View
    ///
    /// - Returns: self
    static func instanceUseHeaderView() -> UseView {
        let array = Bundle.main.loadNibNamed("\(UseView.self)", owner: self, options: nil)
        let view = array?.first as! UseView
        return view
    }
    
    override func awakeFromNib() {
        setUI()
    }
    
    private func setUI() {
        bPlayer.beforView = self
        titleLabel.textColor = ONColor.normalTextColor
        titleLabel.font = UIFont.systemFont(ofSize: 15)
        introductionLabel.textColor = ONColor.subTextColor
        introductionLabel.font = UIFont.systemFont(ofSize: 13)
        levelLabel.layer.cornerRadius = CORNERRADIUS
        levelLabel.layer.borderColor = UIColor.white.cgColor
        levelLabel.layer.borderWidth = 1.0
        followButton.layer.cornerRadius = CORNERRADIUS + 5
        followButton.layer.borderColor = ONColor.subTextColor.cgColor
        followButton.layer.borderWidth = 1.0
        self.userInfoHeight.constant = kHeight(r: self.userInfoHeight.constant)
        userInfoView.layoutIfNeeded()
        MyConfigTool.setBlurImage(userInfoBgImageView, alphaValue: 0.8)
        let height = self.bPlayer.height + self.topView.height + 20 + self.itemsCollectionView.height + 10 + self.commentCountView.height + self.userInfoView.height + kHeight(r: 60)
        self.height = height
        setShadow(view: userInfoView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
         setShadow(view: topView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: 0)
        addCornerWithView(aView: self.commentCountView, aCorners: [.topLeft,.topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
       
        addCornerWithView(aView: userInfoBgImageView, aCorners: [.topLeft, .topRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        addCornerWithView(aView: userInfoBttomView, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))

    }
    
    func setData(model: VideoModel) {
        let url = URL(fileURLWithPath: model.videoFilePath!)
        bPlayer.asset(with: url)
        bPlayer.isHidden = false;
        bPlayer.backgroundColor = UIColor.black
        bPlayer.delegate = self
        introductionLabel.text = "这是一个视频内容啊，内容，内容是啥，啥也没有，哈哈，这是一个视频内容啊，内容，内容是啥，啥也没有，哈哈，这是一个视频内容啊，内容，内容是啥，啥也没有，哈哈"
        titleLabel.appendTextAttributedString(NSAttributedString(string: "This video is so boring.This video is so boring.", attributes: [NSAttributedStringKey.foregroundColor : UIColor.black, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)]))
        titleLabel.appendTextAttributedString(NSAttributedString(string: "今天 19:20", attributes: [NSAttributedStringKey.foregroundColor : ONColor.subTextColor, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 10)])) 
        titleLabelHeight.constant = CGFloat(titleLabel.getHeightWithWidth(ScreenWidth - sideMargin * 2))
        introductionHeight.constant = CGFloat(introductionLabel.getHeightWithWidth(ScreenWidth - sideMargin * 2))
        tagScrollView.showData(titles: ["哈哈","获得很多","哭的粉红色"], labelColor: ONColor.tagLineColor, fontSize: 10) { (index) in
            
        }
        
//        userNameLabel.text = model.userInfoModel.name
//        avatarImageView.kf.setImage(with: nil, placeholder: UIImage(named: model.userInfoModel.haderUrl))
//        userInfoBgImageView.kf.setImage(with: nil, placeholder: UIImage(named: model.userInfoModel.haderUrl))
//        levelLabel.text = "\(model.userInfoModel.level)"
//        itemsCollectionView.setData()
        setSelfHeight()
        itemsCollectionView.contentOffset = .zero
    }
    
    func controlViewIsRight(_ isRight: Bool) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.isLandscape = !isRight
        if isRight {
            self.videoADView?.removeFromSuperview()
            // MARK: - 这句很重要
            let unknownValue = UIInterfaceOrientation.unknown.rawValue
            UIDevice.current.setValue(unknownValue, forKey: "orientation")
            
            // 将视图还原成竖屏
            let orientationTarget = UIInterfaceOrientation.portrait.rawValue
            UIDevice.current.setValue(orientationTarget, forKey: "orientation")
        } else {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.isLandscape = true
            let value = UIInterfaceOrientation.landscapeRight.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
            showVideoADView()
        }
    }
    
    deinit {
        if bPlayer != nil {
            bPlayer.stop()
            bPlayer.removeFromSuperview()
        }
    }
    
    private func setSelfHeight() {
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 0.1) {
            DispatchQueue.main.async {
                let height = self.bPlayer.height + self.topView.height + 20 + self.itemsCollectionView.height + 10 + self.commentCountView.height + self.userInfoView.height
                self.height = height
            }
        }
    }
    
    @IBAction func moreTap(_ sender: UIButton) {
        
    }
    
    // MARK: - 设置旋转后的view
    func showVideoADView() {
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 2) {
            DispatchQueue.main.async {
                if self.videoADView?.superview == nil {
                    self.videoADView = VideoADView()
                    self.videoADView?.showData(data: ["aaa"])
                }
                
            }
        }
    }

}
